﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Data;
using System.Collections.Generic;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;

namespace EntLibContrib.Query.Configuration.Manageability.Parameters
{
	/// <summary>
	/// Represents a mapper for <see cref="DataParameterData"/> configuration to Wmi.
	/// </summary>
	public static class DataParameterDataWmiMapper
	{
		#region Public Static Methods
		/// <summary>
		/// Creates the <see cref="DataParameterSetting"/> instances that describe the
		/// configurationObject.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		public static void GenerateWmiObjects(DataParameterData configurationObject,
																					ICollection<ConfigurationSetting> wmiSettings)
		{
			wmiSettings.Add(new DataParameterSetting(configurationObject,
																							configurationObject.Name,
																							configurationObject.DbType.ToString(),
																							configurationObject.Direction.ToString()));
		}
		#endregion

		#region Internal Static Methods
		/// <summary>
		/// Registers the WMI types.
		/// </summary>
		internal static void RegisterWmiTypes()
		{
			ManagementEntityTypesRegistrar.SafelyRegisterTypes(typeof(DataParameterSetting));
		}

		/// <summary>
		/// Saves the changes.
		/// </summary>
		/// <param name="setting">The setting.</param>
		/// <param name="sourceElement">The source element.</param>
		/// <returns></returns>
		internal static bool SaveChanges(DataParameterSetting setting,
																		ConfigurationElement sourceElement)
		{
			var element = (DataParameterData)sourceElement;

			DbType dbType;
			if (ParseHelper.TryParseEnum<DbType>(setting.DbType, out dbType))
				element.DbType = dbType;

			ParameterDirection direction;
			if (ParseHelper.TryParseEnum<ParameterDirection>(setting.Direction, out direction))
				element.Direction = direction;

			return true;
		}
		#endregion
	}
}
